package wsservice;

import entity.*;
import entityr.WebServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import utility.XMLUtil;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@WebService(serviceName = "macWS", targetNamespace = "http://scc.com.cn", portName = "BasicHttpBinding_IMacIntfWS")
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public class MacIntfWS implements IMacIntfWS {
    DateTimeFormatter dfISO = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    @Autowired
    MacBLL macBLL;

    @Override
    @WebMethod(operationName = "macIntf", action = "http://scc.com.cn/IMacIntfWS/macIntf")
    @WebResult(name = "macIntfResult")
    public WebServiceResponse macIntf(
            @WebParam(name = "methodInvoke") String methodInvoke,
            @WebParam(name = "input") String input) {
        WebServiceResponse webServiceResponse = getUndefinedFunction();
        //
        try {
            if (methodInvoke.equalsIgnoreCase("UserControl")) {
                UserControl mod = XMLUtil.XmlToObject(UserControl.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.isHasAuthority(mod);
                }
            } else if (methodInvoke.equalsIgnoreCase("Ingredients")) {
                Ingredients mod = XMLUtil.XmlToObject(Ingredients.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.Ingredients(mod);
                }
            } else if (methodInvoke.equalsIgnoreCase("LotDataUpload")) {
                LotDataUpload mod = XMLUtil.XmlToObject(LotDataUpload.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.LotDataUpload(mod);
                }
            } else if (methodInvoke.equalsIgnoreCase("DataUpload")) {
                DataUpload mod = XMLUtil.XmlToObject(DataUpload.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.DataUpload(mod);
                }
            } else if (methodInvoke.equalsIgnoreCase("AlarmUpload")) {
                AlarmUpload mod = XMLUtil.XmlToObject(AlarmUpload.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.AlarmUpload(mod);
                }
            } else if (methodInvoke.equalsIgnoreCase("CallAgv")) {
                CallAgv mod = XMLUtil.XmlToObject(CallAgv.class, input);
                if (mod == null) {
                    webServiceResponse = getUnregnisedFormat();
                } else {
                    webServiceResponse = macBLL.CallAgv(mod);
                }
            } else {
                webServiceResponse = getUndefinedFunction();
            }
        } catch (Exception ex) {
            webServiceResponse.setErrorCode("31");
            webServiceResponse.setErrorMsg(ex.getMessage());
            webServiceResponse.setResultData(ex.getLocalizedMessage());
        }
        return webServiceResponse;
    }

    private WebServiceResponse unregnisedFormat;
    private WebServiceResponse undefinedFunction;

    private WebServiceResponse getUnregnisedFormat() {
        if (unregnisedFormat == null) {
            unregnisedFormat = new WebServiceResponse();
            unregnisedFormat.setErrorCode("-12");
            unregnisedFormat.setErrorMsg("NG：格式不正确");
            unregnisedFormat.setResultData(null);
        }
        return unregnisedFormat;
    }

    private WebServiceResponse getUndefinedFunction() {
        if (undefinedFunction == null) {
            undefinedFunction = new WebServiceResponse();
            undefinedFunction.setErrorCode("-11");
            undefinedFunction.setErrorMsg("未定义的功能");
            undefinedFunction.setResultData(null);
        }
        return undefinedFunction;
    }

    //@WebMethod(operationName = "getInput")
    private String getInput(String methodInvoke) {
        String now = LocalDateTime.now().format(dfISO);
        if (methodInvoke.equalsIgnoreCase("UserControl")) {
            UserControl var = new UserControl();
            var.setMacCode("MAC001");
            var.setEmpCode("SN001");
            return utility.XMLUtil.ObjectToXml(var);
        } else if (methodInvoke.equalsIgnoreCase("Ingredients")) {
            Ingredients var = new Ingredients();
            var.setMacCode("MAC001");
            var.setWipEntity("Lot9999");
            return utility.XMLUtil.ObjectToXml(var);
        } else if (methodInvoke.equalsIgnoreCase("LotDataUpload")) {
            LotDataUpload var = new LotDataUpload();
            var.setMacCode("MAC001");
            var.setWipEntity("Lot9999");
            PNLNum p = new PNLNum();
            p.setWipEntityPnl("NG9999");
            Item[] items = new Item[]{
                    new Item("Device001_01", "烘烤温度:550", "2020-10-27 16:52:99"),
                    new Item("Device001_02", "烘烤时长:150", "2020-10-27 16:52:99"),
                    new Item("Device001_03", "烘烤起时:2020-10-27 16:52:00", "2020-10-27 16:52:99"),
                    new Item("Device001_03", "烘烤止时:2020-10-27 16:55:00", "2020-10-27 16:52:99"),
                    new Item("Device001_03", "设备状态:1", "2020-10-27 16:52:99"),
                    new Item("Device001_03", "温度上限:550", "2020-10-27 16:52:99"),
                    new Item("Device001_03", "实际温度:550", "2020-10-27 16:52:99"),
            };
            p.setItems(items);
            var.setPnlNum(new PNLNum[]{p});
            return utility.XMLUtil.ObjectToXml(var);
        } else if (methodInvoke.equalsIgnoreCase("DataUpload")) {
            DataUpload var = new DataUpload();
            var.setMacCode("MAC001");
            var.setWipEntity("Lot9999");
            Item[] items = new Item[]{
                    new Item("MAC001_016", "1", now)
            };
            var.setItem(items);
            return utility.XMLUtil.ObjectToXml(var);
        } else if (methodInvoke.equalsIgnoreCase("AlarmUpload")) {
            AlarmUpload var = new AlarmUpload();
            var.setMacCode("MAC001");
            var.setAlarmCode("ALARM_001");
            var.setAlarmDesc("超温报警");
            var.setTimeStamp("2020-10-27 17:09:56");
            Item[] items = new Item[]{
                    //温度设定值
                    new Item("MAC001_06", "450", now),
                    //温度实际值
                    new Item("MAC001_07", "600", now)
            };
            var.setItem(items);
            return utility.XMLUtil.ObjectToXml(var);
        } else if (methodInvoke.equalsIgnoreCase("CallAgv")) {
            CallAgv var = new CallAgv();
            var.setBerthCode("In_D001");
            var.setMacCode("MAC001");
            var.setWipEntity("Lot99999");
            return utility.XMLUtil.ObjectToXml(var);
        }
        return "定义不符";
    }
}